<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Messages_m extends CI_Model {
        
        function get_inbox($id) {
            $q = $this->db->select('user_messages.msg_id,messages.subject, messages.msg_body, messages.date, coordinator.lname, coordinator.fname')
                    ->from('messages')->join('user_messages', 'messages.msg_id = user_messages.msg_id')
                    ->join('coordinator', 'user_messages.coord_id=coordinator.coordinator_id')
                    ->where('coordinator.coordinator_id', $id)->where('user_messages.msg_type', '1')
                    ->order_by('messages.msg_id', 'DESC')->get();
            if ($q->num_rows() > 0) {
                return $q->result_array();
            } else {
                return FALSE;
            }
        }
        
        function get_inbox_num($id) {
            $q = $this->db->select('*')->from('coordinator')->join('user_messages', 'coordinator.coordinator_id = user_messages.recipient')
                    ->join('messages', 'user_messages.msg_id = messages.msg_id')->where('user_messages.message_status', 0)
                    ->where('coordinator.coordinator_id', $id)->where('user_messages.recipient', $id)->get();
            return $q->num_rows();
        }
        
        function get_sentbox($id) {
            $q = $this->db->select('user_messages.msg_id,messages.subject, messages.msg_body, messages.date, coordinator.lname, coordinator.fname')
                    ->from('messages')->join('user_messages', 'messages.msg_id = user_messages.msg_id')
                    ->join('coordinator', 'user_messages.coord_id=coordinator.coordinator_id')
                    ->where('coordinator.coordinator_id', $id)->where('user_messages.msg_type', '1')
                    ->order_by('messages.msg_id', 'DESC')->get();
            if ($q->num_rows() > 0) {
                return $q->result_array();
            } else {
                return FALSE;
            }
        }
        
        function get_sentbox_num($id) {
            $q = $this->db->select('*')->from('coordinator')->join('user_messages', 'coordinator.coordinator_id = user_messages.sender')
                    ->join('messages', 'user_messages.msg_id = messages.msg_id')->where('user_messages.message_status', 0)
                    ->where('coordinator.coordinator_id', $id)->where('user_messages.sender', $id)->get();
            return $q->num_rows();
        }
        
        function get_message($id) {
            $q = $this->db->select('user_messages.msg_id, messages.subject, messages.msg_body, messages.date, user_messages.attach_name, user_messages.attach_path')
                    ->from('coordinator')->join('user_messages', 'coordinator.coordinator_id = user_messages.recipient')
                    ->join('messages', 'user_messages.msg_id = messages.msg_id')->where('user_messages.message_status', 0)
                    ->where('messages.msg_id', $id)->get();
            if ($q->num_rows() > 0) {
                return $q->row();
            } else {
                return FALSE;
            }
        }
        
        function get_max_msg_id(){
            $q = $this->db->select('msg_id')->from('messages')->order_by('msg_id', 'DESC')->limit(1)->get();
            if ($q->num_rows() > 0) {
                return $q->row();
            } else {
                return FALSE;
            }
        }
        
        function get_unread_msg_num($id) {
            $q = $this->db->select('*')->from('coordinator')->join('user_messages', 'coordinator.coordinator_id = user_messages.recipient')
                    ->join('messages', 'user_messages.msg_id = messages.msg_id')->where('user_messages.message_status', 0)
                    ->where('coordinator.coordinator_id', $id)->where('user_messages.read', 1)->where('user_messages.recipient', $id)->get();
            return $q->num_rows();
        }
        
        function insert_user_messages($msg_id, $data) {
            $user_msg = array(
                'msg_id' => $msg_id,
                'sender' => $data['sender'],
                'recipient' => $data['recipient'],
                'message_status' => $data['message_status'],
                'read' => $data['read'],
                'attach_name' => $data['attach_name'],
                'attach_path' => $data['attach_path'],
            );
            $this->db->insert('user_messages', $user_msg);
            return $this->db->insert_id();
        }
        function insert_msg($data){
            $msg = array(
                'subject' => $data['subject'],
                'msg_body' => $data['msg_body'],
                'date' => $data['date'],
            );
            $this->db->insert('messages', $msg);
            $msg_id = $this->db->insert_id();
            return $this->insert_user_messages($msg_id, $data);
        }
        
        function change_status($data){
            $this->db->where('recipient', $acc_id)->where('msg_id', $msg_id)->update('user_messages', $data);
        }
        
        function delete_msg_inbox($acc_id, $msg_id) {
            $data = array(
                'message_status' => 1,
            );
            for($i=0;$i<sizeof($msg_id);$i++){
                $this->db->where('recipient', $acc_id)->where('msg_id', $msg_id[$i])->update('user_messages', $data);
            }
        }
        
        function delete_msg_sentbox($acc_id, $msg_id) {
            $data = array(
                'message_status' => 1,
            );
            for($i=0;$i<sizeof($msg_id);$i++){
                $this->db->where('sender', $acc_id)->where('msg_id', $msg_id[$i])->update('user_messages', $data);
            }
            
        }
    }
